<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>数据挖掘 | Data Mining Course</title>
    <link rel="stylesheet" href="../../css/main.css" />
    <link rel="stylesheet" href="../../css/tabs.css" />
    <script src="../../js/uikit.js"></script>
    <link href="../../css/prism.css" rel="stylesheet" />
    <script src="../../js/prism.js"></script>
  </head>

  <body>
    <div
      data-uk-sticky="animation: uk-animation-slide-top; sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; cls-inactive: uk-navbar-transparent; top: 300"
    >
      <nav class="uk-navbar-container pt-2 pb-2">
        <div class="uk-container">
          <div data-uk-navbar>
            <div class="uk-navbar-left">
              <a
                class="uk-navbar-item uk-logo uk-visible@m"
                href="../../index.html"
                >数据挖掘课程</a
              >
              <a
                class="uk-navbar-tuk-containeroggle uk-hidden@m"
                href="#offcanvas-docs"
                data-uk-toggle
                ><span data-uk-navbar-toggle-icon></span>
                <span class="uk-margin-small-left">目录</span></a
              >
              <ul class="uk-navbar-nav uk-visible@m">
                <li><a href="../index.html">文档</a></li>
              </ul>
            </div>
            <div class="uk-navbar-center uk-hidden@m">
              <a class="uk-navbar-item uk-logo" href="../../index.html"
                >数据挖掘课程</a
              >
            </div>
          </div>
        </div>
      </nav>
    </div>

    <div class="uk-section">
      <div class="uk-container">
        <div class="uk-grid-large" data-uk-grid>
          <div class="sidebar-fixed-width uk-visible@m">
            <div class="sidebar-docs uk-position-fixed uk-margin-top">
              <h5>关于这门课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../../index.html">大纲</a></li>
              </ul>
              <h5>章节</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../01.html">第1章 - 数据挖掘概念</a></li>
                <li><a href="../02.html">第2章 - 分类</a></li>
                <li><a href="../03.html">第3章 - 聚类</a></li>
                <li><a href="../04.html">第4章 - 关联规则</a></li>
                <li><a href="../05.html">第5章 - 日志的挖掘与应用</a></li>
                <li><a href="../06.html">第6章 - 数据挖掘应用案例</a></li>
              </ul>
              <h5>实验课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="./code-01.html">01</a></li>
                <li><a href="./code-02.html">02</a></li>
                <li><a href="./code-03.html">03</a></li>
                <li><a href="./code-04.html">04</a></li>
                <li class="uk-active"><a href="./code-05.html">05</a></li>
                <li><a href="./code-06.html">06</a></li>
                <li><a href="./code-07.html">07</a></li>
                <li><a href="./code-08.html">08</a></li>
                <li><a href="./code-09.html">09</a></li>
                <li><a href="./code-10.html">10</a></li>
                <li><a href="./code-11.html">11</a></li>
                <li><a href="./code-12.html">12</a></li>
                <li><a href="./code-13.html">13</a></li>
                <li><a href="./code-14.html">14</a></li>
                <li><a href="./code-15.html">15</a></li>
                <li><a href="./code-16.html">16</a></li>
              </ul>
            </div>
          </div>
          <div class="uk-width-1-1 uk-width-expand@m">
            <article class="uk-article">
              <h2>关联规则之Apriori算法</h2>
              <div>
                <p>首先安装需要的包
                  <br>
                  <strong>pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple pandas</strong>
                  <br />
                  <strong>pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple mlxtend</strong>
                  <br />
                  <strong>pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib</strong>
                  <!-- <strong>pip install --user sklearn</strong>
                  <br />
                  -->
                </p>
                
                
                <img src="../../images/lab02/0.JPG" alt="0" />
                <hr />
                <p>新建一个python文件</p>
                <p>1. 引入需要用到的包</p>
<pre><code class="language-python">import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
</code></pre>
<p>
  2. 自定义一份购物数据集， 把下表转化成python接受的数据格式：
  <br>
  <img src="../../images/lab05/orders.JPG" alt="orders" />
</p>
<pre><code class="language-python">data = {'ID': [1, 2, 3, 4, 5, 6],
        'Onion': [1, 0, 0, 1, 1, 1],
        'Potato': [1, 1, 0, 1, 1, 1],
        'Burger': [1, 1, 0, 0, 1, 1],
        'Milk': [0, 1, 1, 1, 0, 1],
        'Beer': [0, 0, 1, 0, 1, 0]}
df = pd.DataFrame(data)
df = df[['ID', 'Onion', 'Potato', 'Burger', 'Milk', 'Beer']]
</code></pre>

<p>3. 找出频繁项集</p>
<p>这里用到了apriori算法，这个算法的具体细节咱们下周讲</p>
<pre><code class="language-python">frequent_itemsets = apriori(df[['Onion', 'Potato', 'Burger', 'Milk', 'Beer']], min_support=0.50, use_colnames=True)
# 可以打印一下看下这些频繁项集
print(frequent_itemsets)
</code></pre>

<p>4. 寻找关联规则</p>
<pre><code class="language-python">rules = association_rules(frequent_itemsets, metric='lift', min_threshold=1)
# 可以打印一下看下这些规则
print(rules)
</code></pre>

<p>5. 挑选</p>
<p>选取相关性系数Lift（提升度）> 1.125并且置信度 > 0.8的规则，即rules[(rules['lift'] > 1.125) & (rules['confidence'] > 0.8)]</p>
<pre><code class="language-python">rules_select = rules[(rules['lift'] > 1.125) & (rules['confidence'] > 0.8)]
print("选取的规则：", rules_select)
</code></pre>
<p>运行后，最后会出现下面的内容，其中antecedents表示前项或者前件，consequents表示前项或者前件。比如这里给出的第一条规则的意思就是 Onion --> Potato. </p>
<img src="../../images/lab05/selected-rules.JPG" alt="selected-rules">
<p>Onion --> Potato的置信度是1，提升度是1.2. 所以可以考虑洋葱和土豆打包或者放在一起出售。</p>


              </div>
            </article>
          </div>
        </div>
      </div>
    </div>

    <div id="offcanvas-docs" data-uk-offcanvas="overlay: true">
      <div class="uk-offcanvas-bar">
        <button class="uk-offcanvas-close" type="button" data-uk-close></button>
        <h5 class="uk-margin-top">关于这门课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../indext.html">大纲</a></li>
        </ul>
        <h5 class="uk-margin-top">章节</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../01.html">第1章 - 数据挖掘概念</a></li>
          <li><a href="../02.html">第2章 - 分类</a></li>
          <li><a href="../03.html">第3章 - 聚类</a></li>
          <li><a href="../04.html">第4章 - 关联规则</a></li>
          <li><a href="../05.html">第5章 - 日志的挖掘与应用</a></li>
          <li><a href="../06.html">第6章 - 数据挖掘应用案例</a></li>
        </ul>
        <h5>实验课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="./code-01.html">01</a></li>
          <li><a href="./code-02.html">02</a></li>
          <li><a href="./code-03.html">03</a></li>
          <li><a href="./code-04.html">04</a></li>
          <li class="uk-active"><a href="./code-05.html">05</a></li>
          <li><a href="./code-06.html">06</a></li>
          <li><a href="./code-07.html">07</a></li>
          <li><a href="./code-08.html">08</a></li>
          <li><a href="./code-09.html">09</a></li>
          <li><a href="./code-10.html">10</a></li>
          <li><a href="./code-11.html">11</a></li>
          <li><a href="./code-12.html">12</a></li>
          <li><a href="./code-13.html">13</a></li>
          <li><a href="./code-14.html">14</a></li>
          <li><a href="./code-15.html">15</a></li>
          <li><a href="./code-16.html">16</a></li>
        </ul>
      </div>
    </div>

    <footer class="uk-section uk-text-center uk-text-muted">
      <div class="uk-container uk-container-small">
        <div class="uk-margin-medium"></div>
      </div>
    </footer>

    <script src="../js/awesomplete.js"></script>
    <script src="../js/custom.js"></script>
  </body>
</html>
